package com.example.toutiao.ui.main.act

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import com.example.toutiao.base.BaseActivity
import com.example.toutiao.base.Constants
import com.example.toutiao.base.Constants.COUNT_TIME
import com.example.toutiao.base.Constants.COUNT_TIME_INTERVAL
import com.example.toutiao.base.Constants.MOBILE
import com.example.toutiao.base.Constants.TOKEN
import com.example.toutiao.ui.main.model.CodeModel
import com.example.toutiao.ui.main.model.RegisterModel
import com.example.toutiao.ui.main.presenter.RegisterPresenter
import com.example.toutiao.ui.main.view.RegisterView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.utils.CommonUtils
import com.example.toutiao.utils.CountDownListener
import com.example.toutiao.utils.SharedPreferencesUtils
import com.example.toutiao.view.AuthCodeTimer
import kotlinx.android.synthetic.main.activity_send_code.*

class SendCodeActivity : BaseActivity<RegisterView, RegisterPresenter>(), RegisterView,
    CountDownListener, TextWatcher {

    private var phone: String? = null

    private var mAuthCodeTimer: AuthCodeTimer? = null
    private var code: String? = null
    private var actionType=0
    /*这两个方法来自 CountDownListener*/

    override fun countDown(time: Long) {
        val s = "重新获取 (${time / 1000})秒"
        tv_send_code.text = s
        tv_send_code.isEnabled = false
    }

    override fun isOverTime(isOverTime: Boolean) {
        tv_send_code.text = "重新获取"
        tv_send_code.isEnabled = true
    }

    override fun getLayoutId(): Int = R.layout.activity_send_code

    override fun initView() {
        icon_back.setOnClickListener {
            finish()
        }
        if (intent != null) {
            phone = CommonUtils.replaceBlank(intent.getStringExtra(MOBILE).toString())
            actionType=intent.getIntExtra(Constants.USER_ACTION,actionType)
        }

        tv_show_update.text = "验证码已发送至：$phone"
        tv_send_code.setOnClickListener {
            getHttpSendCode()
        }
        et_code.addTextChangedListener(this)
        cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
        cd_next.setOnClickListener {
            if (TextUtils.isEmpty(et_code.text.toString())) {
                showToast("请先获取验证码！")
                return@setOnClickListener
            }
            val codeCh: String? = CommonUtils.replaceBlank(et_code.text.toString())
            if (codeCh!!.length == 4) {
                if (codeCh == code) {
                    if(actionType==Constants.ACTION_FIND_PASS){
                        val intent= Intent(this,SetNewPassActivity::class.java)
                        intent.putExtra(Constants.MOBILE,phone)
                        startActivity(intent)

                        finish()
                    }else{
                        getPresenter()!!.getRegister(CommonUtils.replaceBlank(phone!!), code!!,actionType)

                    }
                } else {
                    showToast("验证码错误！")
                }
            }
        }
    }

    private fun getHttpSendCode() {

        if (!TextUtils.isEmpty(phone)) {
            phone?.let { getPresenter()?.getCode(it) }
        }
    }

    override fun initData() {
        getHttpSendCode()
    }

    override fun createPresenter(): RegisterPresenter = RegisterPresenter()
    override fun onRegister(registerModel: RegisterModel) {
        if (registerModel != null) {
            SharedPreferencesUtils.saveToken(this, TOKEN, registerModel.token)
            if(actionType==Constants.ACTION_LOGIN){
                postMsg(EvtMsgModel(102, registerModel.token))
                postMsg(EvtMsgModel(201, registerModel.token))
            }else{
                postMsg(EvtMsgModel(101, registerModel.token))
            }

            finish()
        }

    }

    override fun <T> setData(data: T) {
        if (data != null) {
            /*初始化倒计时*/
            mAuthCodeTimer = AuthCodeTimer(COUNT_TIME, COUNT_TIME_INTERVAL, this)
            mAuthCodeTimer?.start()
            val codeModel: CodeModel = data as CodeModel
            code = codeModel.code
        }

    }
    override fun getMessage(message: EvtMsgModel<*>) {
        super.getMessage(message)
        if (message != null) {
            if (message.code == 104) {
                finish()
            }
        }
    }

    override fun setError(err: String) {


    }


    override fun isRegister(): Boolean = true
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val codeCh: String? = CommonUtils.replaceBlank(s.toString())
        if (codeCh!!.length == 4) {
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.main_color))
            cd_next.isEnabled=true
        } else {
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
            cd_next.isEnabled=false
        }
    }

    override fun afterTextChanged(s: Editable?) {

    }


}